//
//  WithdrawApplyViewController.m
//  阿帕
//
//  Created by mac on 2020/1/2.
//  Copyright © 2020 GCY. All rights reserved.
//

#import "WithdrawApplyViewController.h"
#import "CYPlaceholderTextView.h"
#import "GDBandCardAddViewController.h"
#import "MyViewController.h"

@interface WithdrawApplyViewController ()<UITextFieldDelegate>
@property (weak, nonatomic) IBOutlet UIView *backView;
@property (weak, nonatomic) IBOutlet UILabel *textLabel;
@property (weak, nonatomic) IBOutlet UITextField *withdrawTextField;
@property (weak, nonatomic) IBOutlet UIView *lineView;
@property (strong , nonatomic)CYPlaceholderTextView *textView;
@property (strong , nonatomic)UIButton *confirmButton;

@end

@implementation WithdrawApplyViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self baseViewLayout];
    
}

- (void)baseViewLayout {
    
    self.navigationItem.title = @"提现申请";
    
    self.view.backgroundColor = BackgroundColor;
    _textLabel.textColor = Dark_TextColor;
    _lineView.backgroundColor = BackgroundColor;
    
    _textView = [[CYPlaceholderTextView alloc] initWithFrame:CGRectMake(10, _lineView.cy_bottom + 5, ScreenW - 20, 100)];
    _textView.text = @"";
    _textView.textColor = Light_TextColor;
    _textView.placeholder = @"请填写备注";
    _textView.placeholderColor = Light_TextColor;
    _textView.font = font(14);
    [_backView addSubview:_textView];
    
//    _withdrawTextField.delegate = self;
    
    _withdrawTextField.textFieldType = CJTextFieldStringTypeMoneyValue;
    _withdrawTextField.maxLength = 10;
    _withdrawTextField.text = CJStringWithFormat(@"%.2f", self.balanceFloat);
    
    [self.view addSubview:self.confirmButton];
}

- (void)confirmButtonClick {
    
    WEAKSELF
    
    if ([DCSpeedy isBlankString:_withdrawTextField.text] == YES || [_withdrawTextField.text isEqual:@"0"]) {
        [XSInfoView showInfo:@"请填写提现金额" onView:self.view];
        return;
    }
    
    if ([_withdrawTextField.text floatValue] == 0) {
        [XSInfoView showInfo:@"请填写正确的提现金额" onView:self.view];
        return;
    }
    
    if (self.balanceFloat < [self.withdrawTextField.text floatValue]) {
        [XSInfoView showInfo:@"提现金额大于实际金额" onView:self.view];
        return;
    }
    NSMutableDictionary *parmas = [NSMutableDictionary dictionary];
    [parmas setValue:_withdrawTextField.text forKey:@"totalAmount"];
    [parmas setValue:_textView.text forKey:@"memo"];
    [parmas setValue:[UserDefaultUtil valueForKey:USER_TOKEN] forKey:@"token"];
    [parmas setValue:self.accountType forKey:@"accountType"];
    [parmas setValue:self.dataDic[@"accountId"] forKey:@"accountId"];
    [parmas setValue:self.dataDic[@"taxLocationCode"] forKey:@"taxLocationCode"];

    [HTTPManager postNetworkRequest:CJStringWithFormat(@"%@%@", [NSString stringWithFormat:@"%@/ntocc-tms-api",[ReAddressDynamic shared].baseUrl], MYBANKWITHDRAW_CREATEWITHDRAW) withParameter:parmas withVC:self netName:@"提现申请" success:^(id response, NSString *msg) {
        if ([CJStringWithFormat(@"%@",response[@"code"]) intValue] == 501) {
            UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"提示" message:@"您还未绑定银行卡,是否现在去绑定银行卡?" preferredStyle:UIAlertControllerStyleAlert];
            //取消
            UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"暂不绑定" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action){
               
            }];
            //确定
            UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"去绑定" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action){
                GDBandCardAddViewController *bcavc = [[GDBandCardAddViewController alloc] init];
                NSString *paymentType = [[UserDefaultUtil valueForKey:Login_Branch] objectForKey:@"paymentType"];
                bcavc.isSelect = [paymentType isEqualToString:@"mybank"] ? NO : YES;
                [self.navigationController pushViewController:bcavc animated:YES];
            }];
            [alertController addAction:cancelAction];
            [alertController addAction:okAction];
            
            [self presentViewController:alertController animated:YES completion:nil];
        }
        if ([msg isEqualToString:@"申请成功"]) {
            for (UIViewController *vc in self.navigationController.viewControllers) {
                    if ([vc isKindOfClass:[MyViewController class]]) {
                        [self.navigationController popToViewController:vc animated:YES];
                    }
                }
        }

        [XSInfoView showInfo:msg onView:weakSelf.view];
        
    } failure:^(NSInteger code, NSString *msg) {
        
    }];
}

- (UIButton *)confirmButton {
    if (!_confirmButton) {
        _confirmButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [_confirmButton setTitle:@"提现" forState:UIControlStateNormal];
        [_confirmButton setBackgroundColor:Theme_ColorValue];
        cut(_confirmButton, BTN_BORDER_RADIUS);
        [_confirmButton addTarget:self action:@selector(confirmButtonClick) forControlEvents:UIControlEventTouchUpInside];
        [_confirmButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        _confirmButton.frame = CGRectMake(20, ScreenH - NavHeight - 65, ScreenW-40, 45);
    }
    return _confirmButton;
}

//- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
//
//    NSMutableString * futureString = [NSMutableString stringWithString:textField.text];
//
//    [futureString  insertString:string atIndex:range.location];
//
//    NSInteger flag=0;
//
//    const NSInteger limited = 2;//小数点后需要限制的个数
//
//    for (int i = futureString.length-1; i>=0; i--) {
//
//        if ([futureString characterAtIndex:i] == '.') {
//
//            if (flag > limited) {
//                return NO;
//            }
//            break;
//
//        }
//        flag++;
//
//    }
//    return YES;
//}
    
@end
